package com.ABdolphin.forum.controller;

import com.ABdolphin.forum.common.AppResult;
import com.ABdolphin.forum.common.ResultCode;
import com.ABdolphin.forum.config.AppConfig;
import com.ABdolphin.forum.exception.ApplicationException;
import com.ABdolphin.forum.model.Board;
import com.ABdolphin.forum.model.User;
import com.ABdolphin.forum.services.IBoardService;
import com.ABdolphin.forum.services.impl.BoardServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/board")
@RestController
@Tag(name = "Board")
public class BoardController {
    @Resource(type = BoardServiceImpl.class)
    private IBoardService boardService;
    @PostMapping("/create")
    @Operation(summary = "增加一个新的版块")
    public AppResult create(HttpServletRequest request,
                            @Parameter(name = "name",description = "名称") @RequestParam("name")@NotNull String name,
                            @Parameter(name = "sort",description = "排序优先级")@RequestParam("sort")@NotNull int sort){
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getIsAdmin()==0){
            log.info(ResultCode.FAILED_IS_NOT_ADMIN.toString());
            return AppResult.fail(ResultCode.FAILED_IS_NOT_ADMIN);
        }
        Board board = new Board();
        board.setName(name);
        board.setSort(sort);
        boardService.create(board);
        log.info("管理员id="+user.getId()+", 增加了name={ "+board.getName()+" }的模块");
        return AppResult.success();
    }
    @Operation(summary = "获得版块信息")
    @GetMapping("/getById")
    public AppResult getById(@Parameter(name = "id",description = "版块id")@RequestParam("id")@NotNull Long id){
        Board board = boardService.selectById(id);
        if (board==null){
            log.info(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        if (board.getDeleteState()==1){
            log.info(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            return AppResult.fail(ResultCode.FAILED_BOARD_NOT_EXISTS);
        }
        if (board.getState()==1){
            log.info(ResultCode.FAILED_BOARD_BANNED.toString());
            return AppResult.fail(ResultCode.FAILED_BOARD_BANNED);
        }
        return AppResult.success(board);
    }
    @Value("${forum.board.num:9}")
    private Integer boardNum;

    @GetMapping("/topList")
    @Operation(summary = "获得版块列表信息")
    public AppResult<List<Board>> topList(){
        System.out.println("打印的版块数量: "+boardNum);
        List<Board> boards = boardService.selectByNum(boardNum);
        if (boards==null){
            log.error(ResultCode.FAILED.toString());
            return AppResult.fail(ResultCode.FAILED);
        }
        return AppResult.success(boards);
    }



}
